#include "newaccount.h"
#include "ui_newaccount.h"

NewAccount::NewAccount(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::NewAccount)
{
    ui->setupUi(this);

    NewAcc = QSqlDatabase::addDatabase("QMYSQL");
    NewAcc.setHostName("localhost");
    NewAcc.setUserName("root");
    NewAcc.setPassword("");
    NewAcc.setDatabaseName("petmanage");
    if(NewAcc.open())
    {
        QMessageBox::information(this, "连接提示", "数据库连接成功啦！٩(◕‿◕｡)۶");
    }
    else
    {
        QMessageBox::warning(this, "连接提示", "数据库连接失败啦！(▼皿▼#)");
    }

    ui->NewPasswordInputbox->setEchoMode(QLineEdit::Password);

    ui->MakeSurePasswordInputbox->setEchoMode(QLineEdit::Password);
}

NewAccount::~NewAccount()
{
    delete ui;
}

void NewAccount::on_RegisterInputbox_clicked()
{
    QString NewAccount = ui->NewAccountInputbox->text();
    QString FirstPassword = ui->NewPasswordInputbox->text();
    QString SecondPassword = ui->MakeSurePasswordInputbox->text();
    QString NewOwnerID = ui->NewIDInputbox->text();

    if(FirstPassword != SecondPassword) {
        QMessageBox::warning(this, "输入错误", "请确认两次密码输入是否相同！");
        return;
    }

    if (NewAccount.isEmpty()
        ||FirstPassword.isEmpty()
        ||SecondPassword.isEmpty()
        ||NewOwnerID.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "请完整输入信息！");
        return;
    }

    QString Insert = QString("insert into AccountPassword (Account, Password, OwnerID) values('%1','%2','%3');").arg(NewAccount).arg(FirstPassword).arg(NewOwnerID);

    QSqlQuery Query(NewAcc);;
    if(Query.exec(Insert))
    {
        QMessageBox::information(this, "注册提示", "注册成功！( ﾟ∀ﾟ)ｱﾊﾊ八八ﾉヽﾉヽﾉヽﾉ ＼ / ＼");
    }
    else
    {
        QMessageBox::warning(this, "插入提示", "注册失败！");
    }
}

